﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Manufacturers;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.LuceneHelper;

namespace NopSolutions.NopCommerce.Web.Templates.Lucene
{
    public partial class LuceneManufacturers : BaseLuceneFilterableControl
    {
        public override void BindData(LuceneHelper.FilterEventArgs e)
        {
            var searchManager = new SearchManager();
            if (e.FilterContext.Manufacturers.Count <= 0)
            {
                //We are not filtering by a manufacturer, so lets show all avaible ones
                var visibleManufactures = searchManager.SearchForManufacturersOnProductQuery(e.SearchResult.Query,
                                                                                         ManufacturerManager.
                                                                                             GetAllManufacturers());
                if (visibleManufactures.Count > 0)
                {
                    ddlManufacturers.Items.Add(new ListItem("All", "0"));

                    var currentPageUrl = CommonHelper.GetThisPageUrl(true);
                    foreach (var manu in visibleManufactures)
                    {
                        var listItem = new ListItem(manu.Name + "(" + manu.Total + ")",
                                                    CommonHelper.ModifyQueryString(currentPageUrl,
                                                                                   "ManufacturerID=" +
                                                                                   manu.ManufacturerId, string.Empty));
                        ddlManufacturers.Items.Add(listItem);
                    }

                }
                else
                {
                    this.Visible = false;
                }
            }
            else
            {
                this.Visible = false;
            }
        }

        protected void ddlManufacturers_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(ddlManufacturers.SelectedValue);
        }
    }
}